#include "mytcpsockettodevice.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include "sqlmain.h"

MyTcpSocketToDevice::MyTcpSocketToDevice(QObject *parent)
    : MyTcpSocket{parent}
{

}

void MyTcpSocketToDevice::sendDataToAll(const QByteArray &data)
{
    QJsonObject obj = QJsonDocument::fromJson(data).object();
    int device_type_id = obj.value("device_type_id").toInt();
    int device_type_index = obj.value("device_type_index").toInt();
    QString device = QString("%1_%2")
            .arg(device_type_id)
            .arg(device_type_index);
    if(!deviceList.contains(device))
        return;
    MyTcpSocket::sendDataToAll(data);
}

void MyTcpSocketToDevice::handleFrame(const QJsonObject &obj)
{
    //区分数据类型
    int type = obj.value("type").toInt();
    if(type == 1001){
        //心跳
        handleHeartFrame(obj);
    }
    else if(type == 1002){
        //传感数据
        handleSensorDataFrame(obj);
    }
}

void MyTcpSocketToDevice::handleHeartFrame(const QJsonObject &obj)
{
    int device_type_id = obj.value("device_type_id").toInt();
    int device_type_index = obj.value("device_type_index").toInt();
    QString device = QString("%1_%2")
            .arg(device_type_id)
            .arg(device_type_index);
    deviceList.append(device);
    SqlMain::getObject()->updateDeviceTime(device_type_id,
                                           device_type_index);
}

void MyTcpSocketToDevice::handleSensorDataFrame(const QJsonObject &obj)
{
    int device_type_id = obj.value("device_type_id").toInt();
    int device_type_index = obj.value("device_type_index").toInt();
    QString device = QString("%1_%2")
            .arg(device_type_id)
            .arg(device_type_index);
    if(!deviceList.contains(device))
        deviceList.append(device);

    //类型判断
    if(device_type_id <= 100 || device_type_id >= 501)
        return;
    if(device_type_id <= 104)
    {
        //double
        double value = obj.value("value").toDouble();
        qDebug() << device_type_id << " " << device_type_index << " "
                 << value;
        SqlMain::getObject()->insertValue(device_type_id,
                                          device_type_index,
                                          value);
        SqlMain::getObject()->updateDeviceTime(device_type_id,
                                               device_type_index);
        if(device_type_id == 101 && device_type_index == 1)
        {
            //温度传感器1
            if(value > 30)
            {
                qDebug() << 80;
                QJsonObject reObj;
                reObj.insert("type", 2001);
                reObj.insert("device_type_id", 301);
                reObj.insert("device_type_index", 1);
                reObj.insert("cmd", true);
                emit sendDataToAllSig(
                            QJsonDocument(reObj).toJson());
            }
            else if(value < 25)
            {
                QJsonObject reObj;
                reObj.insert("type", 2001);
                reObj.insert("device_type_id", 301);
                reObj.insert("device_type_index", 1);
                reObj.insert("cmd", false);
                emit sendDataToAllSig(
                            QJsonDocument(reObj).toJson());
            }
        }
    }
    else if(device_type_id <= 109)
    {
        //int
        int value = obj.value("value").toInt();
        qDebug() << device_type_id << " " << device_type_index << " "
                 << value;
        SqlMain::getObject()->insertValue(device_type_id,
                                          device_type_index,
                                          value);
        SqlMain::getObject()->updateDeviceTime(device_type_id,
                                               device_type_index);
    }
    else if(device_type_id == 110)
    {
        //json
        SqlMain::getObject()->updateDeviceTime(device_type_id,
                                               device_type_index);
    }
    else if(device_type_id >= 201 && device_type_id <= 208)
    {
        //bool
        bool value = obj.value("value").toBool();
        qDebug() << device_type_id << " " << device_type_index << " "
                 << value;
        SqlMain::getObject()->insertValue(device_type_id,
                                          device_type_index,
                                          value);
        SqlMain::getObject()->updateDeviceTime(device_type_id,
                                               device_type_index);
    }
}
